#include<vector>
using std::vector;
class Solution {
public:
    int __removeDuplicatesK(vector<int>&nums,int k){
        if(nums.size()<=k)return nums.size();
        //判断j和已保留的后k个元素比较来确定j是否也保留
        int i,j;
        for(i=k-1,j=k;j<nums.size();){
            bool skip=true;
            for(int t=0;t<k;t++){
                if(nums[i-t]!=nums[j])
                {skip=false;break;}
            }
            if(skip){j++;}
            else{
                nums[++i]=nums[j++];
            }
        }
        return i+1;
    }
    int removeDuplicates(vector<int>& nums) {
        return __removeDuplicatesK(nums,2);
    }
};
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int i=1,j=1;
        int count=1;
        while(j<nums.size()){
            if(nums[j-1]==nums[j]){
                if(count==2){
                    j++;
                    continue;
                }
                count++;
            }
            else{
                count=1;
            }
            nums[i++]=nums[j++];
        }
        return i;
    }
};